using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Dialogs/Date Picker Year List", 100)]
	public class DialogDatePickerYearList : MonoBehaviour
	{
		[SerializeField]
		private DialogDatePicker m_DatePicker;

		[SerializeField]
		private GameObject m_YearTemplate;

		[SerializeField]
		private ScrollRect m_ScrollRect;

		private List<DialogDatePickerYearItem> m_YearItems = new List<DialogDatePickerYearItem>();

		private void Awake()
		{
			List<int> list = new List<int>();
			for (int i = 1900; i < 2100; i++)
			{
				list.Add(i);
			}
			m_YearItems.Clear();
			for (int j = 0; j < list.Count; j++)
			{
				m_YearItems.Add(CreateYearItem(j, list[j]));
			}
			UnityEngine.Object.Destroy(m_YearTemplate);
		}

		private DialogDatePickerYearItem CreateYearItem(int i, int year)
		{
			DialogDatePickerYearItem component = UnityEngine.Object.Instantiate(m_YearTemplate).GetComponent<DialogDatePickerYearItem>();
			RectTransform component2 = component.GetComponent<RectTransform>();
			component2.SetParent(m_YearTemplate.transform.parent);
			component2.localScale = Vector3.one;
			component2.localEulerAngles = Vector3.zero;
			component.year = year;
			component.index = i;
			DialogDatePickerYearItem dialogDatePickerYearItem = component;
			dialogDatePickerYearItem.onClickAction = (Action<int>)Delegate.Combine(dialogDatePickerYearItem.onClickAction, new Action<int>(OnItemClick));
			return component;
		}

		public void SetColor(Color accentColor)
		{
			for (int i = 0; i < m_YearItems.Count; i++)
			{
				m_YearItems[i].selectedImage.color = accentColor;
			}
		}

		public void OnItemClick(int index)
		{
			Toggle toggle = m_YearItems[index].toggle;
			toggle.isOn = !toggle.isOn;
			if (toggle.isOn)
			{
				m_DatePicker.SetYear(m_YearItems[index].year);
			}
		}

		public void CenterToSelectedYear(int year)
		{
			int num = 0;
			for (int i = 0; i < m_YearItems.Count; i++)
			{
				m_YearItems[i].UpdateState(year);
				if (m_YearItems[i].toggle.isOn)
				{
					num = i;
				}
			}
			float num2 = 0f;
			if (num <= 3)
			{
				num2 = 0f;
			}
			else if (num >= m_YearItems.Count - 3)
			{
				num2 = 1f;
			}
			else
			{
				num2 = num - 3;
				num2 /= (float)(m_YearItems.Count - 6);
			}
			m_ScrollRect.verticalNormalizedPosition = 1f - num2;
		}
	}
}
